#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Solution {
public:
    int maximumRequests(int n, vector<vector<int>>& requests) {
        int ret = requests.size();
        vector<vector<int>> arr(n, vector<int>(0, 0));
        vector<int> history{};

        int fromPos;
        int toPos;
        for (const auto& request : requests) {
            fromPos = request[0];
            toPos = request[1];
            if (fromPos == toPos) {
                continue;
            }

            auto it = find(arr[toPos].begin(), arr[toPos].end(), fromPos);
            if ( it != arr[toPos].end()) {
                arr[toPos].erase(it);
                auto hisIt1 = find(history.begin(), history.end(), fromPos);
                history.erase(hisIt1);
                auto hisIt2 = find(history.begin(), history.end(), toPos);
                history.erase(hisIt2);
                continue;
            }

            if (!isMergeProc(history, fromPos, toPos, arr)) {
                arr[fromPos].push_back(toPos);
                history.push_back(fromPos);
                history.push_back(toPos);
            }
        }
        ret = requests.size();
        for (int idx = 0; idx < n; idx++) {
            ret -= arr[idx].size();
        }
        return ret;
    }

    bool isMergeProc(vector<int>& vt, int fromPos, int toPos, vector<vector<int>>& arr)
    {
        auto it = find(vt.begin(), vt.end(), fromPos);
        if (it == vt.end()) {
            return false;
        }

        int idx = it - vt.begin();
        if (idx % 2 != 1) {
            return false;
        }
        
        int mergePos = idx - 1;
        auto inter = find(arr[mergePos].begin(), arr[mergePos].end(), fromPos);
        arr[mergePos].erase(inter);
        arr[mergePos].push_back(toPos);
    
        vt[idx] = toPos;

        return true;
    }
};

int main()
{
    Solution s;
    vector<vector<int>> vt{{1,1},{1,0},{0,1},{0,0},{0,0},{0,1},{0,1},{1,0},{1,0},{1,1},{0,0},{1,0}};
    int ret = s.maximumRequests(2, vt);
    cout << ret << endl;

    return 0;
}